package org.price.sql.repo.documents.config;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

/**
 * Hello world!
 *
 */
public class Main2 {
    private static final Logger LOG = Logger.getLogger(Main2.class);

    static {
        BasicConfigurator.configure();

    }

    public static void main(String[] args) throws Exception {

        Class.forName("org.postgresql.Driver");
        Connection conn = DriverManager.getConnection("jdbc:postgresql:gigi",
                "gigi", "gigi");

        DatabaseMetaData metadata = conn.getMetaData();
        LOG.info("DB: " + metadata.getDatabaseProductName());
        LOG.info("Version: " + metadata.getDatabaseProductVersion());

        ResultSet rs = metadata.getCatalogs();
        while (rs.next()) {
            LOG.info("Database Name: " + rs.getString("TABLE_CAT"));
        }
        rs.close();

        String table = null;

        rs = metadata.getTables(null, null, null, new String[] { "TABLE" });
        while (rs.next()) {
            table = rs.getString("TABLE_NAME");
            LOG.info("Table Name: " + table);
        }
        rs.close();

        rs = metadata.getColumns(null, null, table, null);
        while (rs.next()) {

            LOG.info("====================================");
            LOG.info("Column: " + rs.getString("COLUMN_NAME"));
            LOG.info(rs.getString("DATA_TYPE"));
            LOG.info(rs.getString("TYPE_NAME"));
            LOG.info(rs.getString("COLUMN_SIZE"));
            LOG.info(rs.getString("NULLABLE"));
            LOG.info("====================================");
        }

        conn.close();
    }
}
